package com.lyq.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @className: UploadPoiUtils
 * @description: 解析excel中的图片 配合poi使用
 * @author: XTJ
 * @date: 2023/05/22 17:14
 **/
@Component
public class PoiPictureUtils {

    /**
     * 适用于单行单列 单张图片 获取图片和位置 (xls)
     * @param sheet
     * @return
     * @throws IOException
     */
    public static Map<String, PictureData> getPictures1(HSSFSheet sheet) throws IOException {
        Map<String, PictureData> map = new HashMap<String, PictureData>();
        List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren();
        for (HSSFShape shape : list) {
            if (shape instanceof HSSFPicture) {
                HSSFPicture picture = (HSSFPicture) shape;
                HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
                PictureData pdata = picture.getPictureData();
                String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); // 行号-列号
                map.put(key, pdata);
            }
        }
        return map;
    }

    /**
     * 适用于单行单列 单张图片 获取图片和位置获取图片和位置 (xlsx)
     *
     * @param sheet
     * @return
     * @throws IOException
     */
    public static Map<String, PictureData> getPictures2(XSSFSheet sheet) throws IOException {
        Map<String, PictureData> map = new HashMap<String, PictureData>();
        List<POIXMLDocumentPart> list = sheet.getRelations();
        for (POIXMLDocumentPart part : list) {
            if (part instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing) part;
                List<XSSFShape> shapes = drawing.getShapes();
                for (XSSFShape shape : shapes) {
                    XSSFPicture picture = (XSSFPicture) shape;
                    XSSFClientAnchor anchor = picture.getPreferredSize();
                    CTMarker marker = anchor.getFrom();
                    String key = marker.getRow() + "-" + marker.getCol();
                    map.put(key, picture.getPictureData());
                }
            }
        }
        return map;
    }

    /**
     * 适用于多张 ，单元格多张图片 获取图片和位置 (xls)
     *
     * @param sheet
     * @return
     * @throws IOException
     */
    public static Map<String, List<PictureData>> getPictures3(HSSFSheet sheet, HSSFWorkbook workbook) {
        Map<String, List<PictureData>> maps = new HashMap<String, List<PictureData>>();
        List<HSSFPictureData> pictures = workbook.getAllPictures();
        List<PictureData> list = new ArrayList<>();
        if (pictures.size() != 0) {
            String picIndex = "";
            String flag = "";
            for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
                HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
                if (shape instanceof HSSFPicture) {
                    HSSFPicture pic = (HSSFPicture) shape;
                    int pictureIndex = pic.getPictureIndex() - 1;
                    HSSFPictureData picData = pictures.get(pictureIndex);
                    picIndex = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1());

                    //判断循环位置
                    if (flag == null || !picIndex.equals(flag)) {
                        flag = String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1());
                        list = new ArrayList<>();
                    }

                    list.add(picData);
                    maps.put(picIndex, list);
                }
            }
            return maps;
        } else {
            return null;
        }
    }




}
